package com.apestech.framework.dictionary;

import com.apestech.framework.cache.Cache;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.dictionary.lib.ReadDictionary;
import com.apestech.framework.dictionary.lib.WriteDictionary;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.FileUtil;

/**
 * @author hup
 * @create 2018-08-31-9:48
 */
public class DataProcedure extends DictUtil {

    private static String getCacheKey(String procName, String type){
        String key="DICT_PROC_";
        if("json".equals(type)){
            key=key+"JSON_";
        }
        return key+procName;
    }

    private static String getProcFile(SqlSession session, String procName){
        return getDictDir(session)+"\\proc\\"+procName;
    }

    private static Object getProcedure(SqlSession session, String procName, String type) throws Exception{
        Object obj=Cache.getKey(getCacheKey(procName,type));
        if(obj==null){
            obj=getProcedureFromDataBase(session, procName, type);
            Cache.setKey(getCacheKey(procName, type), obj);
        }
        return obj;
    }

    private static Object getProcedureFromFile(SqlSession session, String procName, String type) throws Exception {
        String pname=procName;
        int pos=procName.indexOf(".");
        if(pos>=0) {
            pname = procName.substring(pos + 1, pname.length());
        }
        String file=getProcFile(session,pname);
        if(!FileUtil.fileExists(file)) return null;
        ReadDictionary read=new ReadDictionary(getDictDir(session), "proc");
        Object obj;
        if("json".equals(type)){
            obj=read.getProcedureJson(procName);
        } else {
            obj=read.getProcedure(procName);
        }
        read.close();
        return obj;
    }

    private static Object getProcedureFromDataBase(SqlSession session, String procName, String type) throws Exception {
        WriteDictionary writer=new WriteDictionary(session, getDictDir(session));
        writer.getProcedure(procName);
        return getProcedureFromFile(session, procName, type);
    }

    private static Object getProcedureALL(String procName, String type, SqlSession session) throws Exception {
         Object obj=Cache.getKey(getCacheKey(procName,type));
        if(obj==null){
            obj=getProcedureFromFile(session, procName, type);
            if(obj!=null){
                Cache.setKey(getCacheKey(procName,type), obj);
            }
        }
        return obj;
    }

    public static JSONArray getProcedure(SqlSession session, String procName) throws Exception{
        return (JSONArray) getProcedure(session, procName, null);
    }

    public static JSONObject getProcedureJson(SqlSession session, String procName) throws Exception{
        return (JSONObject) getProcedure(session, procName, "json");
    }

    public static void clearProcedure(SqlSession session, String procName) throws Exception {
        String file=getProcFile(session,procName);
        Cache.removeKey(getCacheKey(procName,null));
        Cache.removeKey(getCacheKey(procName,"json"));
        FileUtil.deleteDir(file+"/proc/"+procName);
    }

    public static JSONArray refreshProcedure(String procName, SqlSession session) throws Exception {
        clearProcedure(session, procName);
        return getProcedure(session, procName);
    }

    public static JSONObject refreshProcedureJson(String procName, SqlSession session) throws Exception {
        clearProcedure(session, procName);
        return getProcedureJson(session, procName);
    }

}
